package com.example.myapplication;

import android.content.SharedPreferences;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.inter.AdResultCallBack;
import com.example.inter.AdapterCallback;
import com.example.inter.AdviceCallBack;
import com.example.inter.LoginCallBack;
import com.example.util.SendAdviceUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AdviceActivity extends AppCompatActivity implements View.OnClickListener, LoginCallBack,AdapterCallback{
    private ImageView btn_reply;
    private EditText textView;
    private String paperId;
    private String advice_id;
    private String tag;
    private SendAdviceUtil sendAdviceUtil;
    private int postion;

    public String Info;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advice);
        initView();
        btn_reply.setOnClickListener(this);
        callBack1();
    }


    public void callBack1() {

        Bundle bundle = getIntent().getExtras();
        paperId = bundle.getString("paperId");
        advice_id = bundle.getString("adviceId","0");
        postion = bundle.getInt("position",-1);
        tag = bundle.getString("tag","0");
    }

    public void initView() {

        textView = (EditText) findViewById(R.id.edit_text);
        btn_reply = (ImageView) findViewById(R.id.btn_reply1);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reply1:
                if (checkEdit()) {
                    String date = getDate();
                    String advice_commit = textView.getText().toString().trim();
                    String student_number = getStudentNumber();
                    sendAdviceUtil = new SendAdviceUtil();
                    if(postion>=0){
                        tag = 1+"";
                    }else{
                        tag = "0";
                    }
                    Log.i("sjsjs", "advice_commit" + advice_commit + "," + student_number + "," + paperId + "" + date);
                    sendAdviceUtil.getInfo(student_number, date, tag, advice_commit, advice_id, paperId, this);
                    postion = -1;
                } else {
                    textView.setError("不能为空和null");
                }
                break;
        }
    }

    public String getStudentNumber() {
        SharedPreferences sharedPreferences = getSharedPreferences("student_info", MODE_PRIVATE);
        String student_number = sharedPreferences.getString("username", "");
        Log.i("student_number", student_number);
        return student_number;
    }

    public String getDate() {
        SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date = new Date();
        String a = form.format(date);
        return a;
    }

    public boolean checkEdit() {
        String edit = textView.getText().toString().trim();
        if (edit.equals("") || edit.equals("null")) {
            return false;
        }
        return true;
    }

    @Override
    public void callBack(int postion, int tag) {
        this.postion = postion;
        this.tag = tag+"";
    }

    @Override
    public void onSuccess(String info) {
        Log.i("TAG", info);
        try {
            JSONObject jsonObject = new JSONObject(info);
            String errcode = jsonObject.getString("errcode");
            Info = info;
            if (errcode.equals("0"))
                Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();
            finish();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onFailed() {

    }


}
